package com.atguigu.gulimall.order.vo;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000117694566";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCVWiAbHvXOwdaSBClPYVs/gJOiN6Iw32LkpR+mdpk9iWrBsgmDiZLqFOZTbsjmLDUSZTi2oDQN08vybNJF/mOU7oS5BZw7GHS3zeaXBckAbCT3AIz8m7Q1HrkVDGV4Y/r3cFs+QBvspRAUDB8RpU/mth5+m5pGK/iLT9lRUwVpq17928ncc7EfBWhDV+4InV9R+rFpySDa2P34DO4e7jYWs14nEQUI96gW1EPjkHnMdwp+bf510cUKmCZmk5cn1THDv8dyZLKmY9So+w6jm8/K0oLmuAcNF7aUj4LXzbRgvF7GEyBHATvDIncE+N2+nGeUW6M86UDKGgm6imdOFXNBAgMBAAECggEBAIfQKRoAkW6u/jxR5G73DU810QrWpHrk6L0bGyj4+GjpKVdyWwxuclbDIqAEQlRtJuXPfj80d6GLVA7jvmM/7bZlH/RQkb17g8gRSPtUm2/y57plmc+sl7cmdRAa8bsE0Bdq6M425i3P/Er+qtIVDg+y1RA015mwKpgkyG4XnPh1GzRGuwP2GFwmZiUCyAoPClHvuY/BOKPlTpOx4g2TZA7a8aOShdir07JZE3reB1iMuZjgWNg2u7vmvl7hh3195AGWN+aZZQU7Uop9dYX7v26L+su/lDdoDeOVnt3jv7JlHLlKRwHUx9iNERiwPYUTJncS1NuqSr/8R4JPCtV4RvECgYEA+5l2q9gXGR+CVPLqpKTShIWWiEY+1sjEH7v1ieD1zpJA8Wj5UO00CMRV+1Y0PuqG1qKjVRQLNcUl51ca149DAjhKZtusi3GEtJM2fXVcm2nqjZj9zJ0MB3zCsmN4MRT+W7YNh5LwJQcDqpwg3WFLa3DdtkILy5Ebk2B6mcoGHxsCgYEAl/bZYEuGuTgZTuNQnzqyNqsVQu6Jr66sVlFlQShzqeIYWkuJfwkdNaiXPdSktn+jQDBJQP8mky1y9U6IB9FRhaXXT3RU9gTy0To2LUSCgHgoBoWCJSuXBv/d/HGBQJoXH5v+YO3RTPlNgp72YzWAfwHTehrp8GkOpnfuTzkqcNMCgYA/Kup2foxeDGEmfs6Wg6MvDnMax7gjKMjExuYAtfBA6stPUPG3dbZotRJ5Rc1cNnmgDblANQYTe+nmSSI+GIPUXNmhNWn+oIOW6pb1rK+RfWgcINsnsg5BGvuu2fZlLEYoNG71KNkOEtLC1OwTaMgMs4uWpMJdW5M2zzQ4xFYBHQKBgHMftiSGcyUEg9/Iw/4tKG/UAkJ3leV7LeIivdO0ApPzzkExspOD4xYQh6t22eXMIm/xZL2RzrZ3Gv0OJm3NvLXEIq510hujOhUZxYtbuIX9AU2XfTYNpNfPpWumzaWUVupCcl8aK5Ll1zuak8wq7xLI3l5F326bGFcyyl5IT4ezAoGAJyAmyAMo0TL/SoMGNGV39nXDztgc46X/gEIngTmrDBdlg2Bu+6wKItqzuGosdm6ILnyJ7VddghDmThR+/HEeKrL17vFFuqs+cXuRj4rn2Yrn7ZoKBaoTSDMK6xmF750GdNsJMR1WhEUW0xM+Tboh6tAEliE3ImrKWpILEju2EzQ=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsr6tHhhKj2FhMmI7Y6L7uGL/y9I/b+nkTIenuiJpk9LzJt0eWJbGNrasoceaAvc/a+uFjp1lwPYplTHPEtKr3vt+qXNZN2iqX7KXAPaTs4fCu6N7PKhRUwX6Dlac31klMna7wsShze9b3c1b/aKMvYMXtZZKUAYqJQMCoGCzgdAa7MmGj3HQCYIHaY4kC5aug7nVCG2YAH6xy3X769iOmiFjGpD3LgoJU0SXXtIPldtk1EW4pMahF72XywKU7dVq6+0+ORF7VrVR+8KKPcSesUYD8alJ7DMCcw5LZ0KhmjBgSfk8LzOireJbwsCe2GmwKQRQwmw5W0b9p8GOdkvnmwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://scvdk0ml4f.cdhttp.cn/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://order.gulimall.com/orderList.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
